home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Developers / src / kant-generator-04-c / Kant ƒ / Kode ƒ / kant load-save.c next >
Encoding:
C/C++ Source or Header  |  1994-11-23  |  2.3 KB  |  122 lines  |  [TEXT/MMCC]

  1. #include <Script.h>
  2. #include "kant load-save.h"
  3. #include "kant.h"
  4. #include "kant text twiddling.h"
  5. #include "file interface.h"
  6. #include "program globals.h"
  7. #include "error.h"
  8. #include "graphics.h"
  9. #include "dialogs.h"
  10. #include "environment.h"
  11. #include "util.h"
  12.  
  13. FSSpec            saveFile;
  14. Boolean            deleteTheThing;
  15.  
  16. void InitLoadSave(void)
  17. {
  18.     saveFile.name[0]=0x00;
  19.     deleteTheThing=FALSE;
  20. }
  21.  
  22. void LoadSaveDispatch(Boolean isLoad, Boolean useOldFile)
  23. {
  24.     if (isLoad)
  25.     {
  26.         if (GetSourceFile(&saveFile))
  27.             HandleError(GetTheFile(&saveFile), FALSE);
  28.     }
  29.     else
  30.     {
  31.         useOldFile=useOldFile&&(saveFile.name[0]!=0x00);
  32.         if (useOldFile ? TRUE : GetDestFile(&saveFile, &deleteTheThing, "\pSave text as..."))
  33.             HandleError(SaveTheFile(saveFile), FALSE);
  34.     }
  35. }
  36.  
  37. enum ErrorTypes SaveTheFile(FSSpec saveFile)
  38. {
  39.     OSErr            theError;
  40.     short            thisFile;
  41.     long            count;
  42.     TEHandle        hTE;
  43.     
  44.     if (deleteTheThing)
  45.     {
  46.         FSpDelete(&saveFile);
  47.     }
  48.     FlushVol(0L, saveFile.vRefNum);
  49.     
  50.     theError=FSpCreate(&saveFile, CREATOR, SAVE_TYPE, smSystemScript);
  51.     FlushVol(0L, saveFile.vRefNum);
  52.     
  53.     if (theError!=noErr)
  54.         return kCantCreateFile;
  55.     
  56.     theError=FSpOpenDF(&saveFile, fsRdWrPerm, &thisFile);
  57.     
  58.     if (theError!=noErr)
  59.         return kCantOpenFileToSave;
  60.  
  61.     hTE=GetTheTextHandle();
  62.     count=(**hTE).teLength;
  63.     theError=SetEOF(thisFile, count);
  64.     if (theError!=noErr)
  65.     {
  66.         FSClose(thisFile);
  67.         return kDiskFull;
  68.     }
  69.     
  70.     SetFPos(thisFile, 1, 0L);
  71.     theError=FSWrite(thisFile, &count, *((**hTE).hText));
  72.     
  73.     FSClose(thisFile);
  74.     FlushVol(0L, saveFile.vRefNum);
  75.  
  76.     if (theError!=noErr)
  77.     {
  78.         FSpDelete(&saveFile);
  79.         
  80.         saveFile.name[0]=0x00;
  81.         return kCantWriteFile;
  82.     }
  83.     else deleteTheThing=TRUE;
  84.     
  85.     SetWTitle(GetIndWindowPtr(kMainWindow), saveFile.name);
  86.     
  87.     return allsWell;
  88. }
  89.  
  90. enum ErrorTypes GetTheFile(FSSpec *saveFile)
  91. {
  92.     short            thisFile;
  93.     long            count;
  94.     OSErr            theError;
  95.     Ptr                data;
  96.     
  97.     theError=FSpOpenDF(saveFile, fsRdPerm, &thisFile);
  98.     if (theError!=noErr)
  99.         return kCantOpenFileToLoad;
  100.     
  101.     GetEOF(thisFile, &count);
  102.     if (count>32767)
  103.         return kFileTooLarge;
  104.     
  105.     data=NewPtr(count);
  106.     SetFPos(thisFile, 1, 0L);
  107.     theError=FSRead(thisFile, &count, data);
  108.     FSClose(thisFile);
  109.     
  110.     if (theError!=noErr)
  111.         return kCantLoadFile;
  112.     
  113.     deleteTheThing=TRUE;
  114.     
  115.     OpenTheIndWindow(kMainWindow);
  116.     SetWTitle(GetIndWindowPtr(kMainWindow), saveFile->name);
  117.     SetTheText(data, count);
  118.     DisposePtr(data);
  119.     
  120.     return allsWell;
  121. }
  122.